package com.atguigu.day03;

import org.apache.flink.api.common.eventtime.WatermarkStrategy;
import org.apache.flink.api.common.typeinfo.Types;
import org.apache.flink.api.connector.source.util.ratelimit.RateLimiterStrategy;
import org.apache.flink.connector.datagen.source.DataGeneratorSource;
import org.apache.flink.connector.datagen.source.GeneratorFunction;
import org.apache.flink.streaming.api.datastream.DataStreamSource;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;

/**
 * @author Felix
 * @date 2024/4/1
 * 该案例演示了数据生成连接器，主要用于生成一些测试数据
 */
public class Flink07_Source_DataGen {
    public static void main(String[] args) throws Exception {
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment();

        env.setParallelism(2);

        DataGeneratorSource<String> dataGeneratorSource = new DataGeneratorSource<>(
                new GeneratorFunction<Long, String>() {
                    @Override
                    public String map(Long value) throws Exception {
                        //参数就是我们DataGen生成的随机数
                        return "数据:" + value;
                    }
                },
                100,
                RateLimiterStrategy.perSecond(10),
                //TypeInformation.of(String.class)
                Types.STRING
        );
        DataStreamSource<String> dataDS = env.fromSource(dataGeneratorSource, WatermarkStrategy.noWatermarks(), "data_source");
        dataDS.print();

        env.execute();
    }
}
